﻿@model XPressify.Models.AlbumEditPhotoModel
@using XPressify.Data.Helpers
@using XPressify.Controllers
@using Omu.Awesome.Core;
@using Omu.Awesome.Mvc.Helpers;

@{
    ViewBag.Title = Model.AlbumName;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">
    
    function checkAll() {
        var element = document.getElementsByName('DeleteBox');
        var i = 0;
        while(true){
            var element = document.getElementsByName('UploadedFiles['+ i + '].Selected');
            if (element[0] == null) {
                break;
            }
            element[0].checked = true;
            i++;
        }
        var btn = document.getElementById("checkBtn")
        btn.onclick = uncheckAll;
        btn.value = "Uncheck all"
    }
    function uncheckAll() {
        var element = document.getElementsByName('DeleteBox');
        var i = 0;
        while (true) {
            var element = document.getElementsByName('UploadedFiles[' + i + '].Selected');
            if (element[0] == null) {
                break;
            }
            element[0].checked = false;
            i++;
        }
        var btn = document.getElementById("checkBtn")
        btn.onclick = checkAll;
        btn.value = "Check all"
    }

    

    

</script>



@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend style="font-size:x-large;color:#222">@Model.AlbumName</legend>
        <div>
            @if (Model.isProfileAlbum == false && Model.isWallAlbum == false)
            {
                @(Html.MakePopupForm<AlbumController>(o => o.AlbumInfoEdit(Model.Id), title: "Edit", height: 550, width: 600, okText: "Save", resizable: false, refreshOnSuccess: true))
                @(Html.PopupFormActionLink<AlbumController>(o => o.AlbumInfoEdit(Model.Id), "Edit", new { @class = "nobtn" }))
            }
            else
            {
                @(Html.MakePopupForm<AlbumController>(o => o.AlbumPrivacyEdit(Model.Id), title: "Edit", height: 365, width: 500, okText: "Save", resizable: false, refreshOnSuccess: true))
                @(Html.PopupFormActionLink<AlbumController>(o => o.AlbumPrivacyEdit(Model.Id), "Edit", new { @class = "nobtn" }))
            }
            @if (Model.isProfileAlbum == false && Model.isWallAlbum == false)
            { 
                <br />
                <div>
                    @(Html.MakePopupForm<AlbumController>(o => o.AlbumUploadFileNormal(Model.Id), title: "Upload", height: 340, width: 600, okText: "", resizable: false, refreshOnSuccess: false))
                    @(Html.PopupFormActionLink<AlbumController>(o => o.AlbumUploadFileNormal(Model.Id), "Upload files", new { @class = "nobtn" }))
                </div>
            }
        <div align="right"><input type="button" class="abtn" id="checkBtn" name="UnCheckAll" value="Check All" onclick="checkAll()" /></div>
    </div>
        <table>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Description</th>
                <th>Size</th>
                <th>Delete</th>
                <th>Cover</th>
                @if (Model.OwnerIsUser)
                {
                    <th>Profile Picture</th>    
                }
                <th></th>
            </tr>
            
            @{
                var index = 0;
                foreach (var File in Model.UploadedFiles)
                { 
                    <tr>
                        <td class="nobtn">
                            @if (File.MediaType == MediaType.Photo)
                            {
                                <img src="@File.Path" alt="@File.Name" title="@File.Name" style="width: 100px;" class="nobtn"/>
                            }
                            else if (File.MediaType == MediaType.Video)
                            {
                                <a class="nobtn"@*"media {width:480, height:280}"*@ href="@File.Path">@File.FullName</a>
                            }
                            else if (File.MediaType == MediaType.Audio)
                            {
                                <a href="@File.Path" class="nobtn">@File.FullName</a>
                            }
                            else
                            {
                                <a href="@File.Path" class="nobtn">@File.FullName</a>
                            }
                        </td>
                        <td>
                            @if (!Model.isProfileAlbum)
                            {
                                @Html.TextBoxFor(model => model.UploadedFiles[index].Name)
                            }
                            else
                            {
                                @Html.HiddenFor(model => model.UploadedFiles[index].Name)
                            }
                        </td>
                        <td>
                            @Html.TextAreaFor(model => model.UploadedFiles[index].Description, 4, 22, new { style = "resize: none;" })
                        </td>
                        <td>
                            @((float)File.Size / 1024) KB
                            @Html.HiddenFor(model => model.UploadedFiles[index].Size)
                        </td>
                        
                        <td>
                            @Html.CheckBoxFor(model => model.UploadedFiles[index].Selected)
                        </td>
                        <td>
                        @if (File.MediaType == MediaType.Photo && Model.isProfileAlbum == false)
                        {
                            if ((Model.HasCover && File.Id == Model.CoverPicture) || (Model.HasCover == false && index == 0 && File.MediaType == MediaType.Photo))
                            {
                                    @Html.RadioButtonFor(model => model.SelectedCoverPicture, Model.UploadedFiles[index].Id, new { Checked = "checked", name = "DeleteBox" })
                            }
                            else
                            {
                                    @Html.RadioButtonFor(model => model.SelectedCoverPicture, Model.UploadedFiles[index].Id)
                            }
                        }
                        </td>
                        
                        @if (File.MediaType == MediaType.Photo && Model.OwnerIsUser)
                        {
                            <td>
                                @Html.RadioButtonFor(model => model.SelectedProfilePicture, Model.UploadedFiles[index].Id)
                            </td>
                        }
                        
                        <td>
                            @Html.HiddenFor(model => model.UploadedFiles[index].Id)
                            @Html.HiddenFor(model => model.CoverPicture)
                            @if (Model.SelectedProfilePicture == Guid.Empty && index == 0)
                            {
                                @Html.RadioButtonFor(model => model.SelectedProfilePicture, Guid.Empty, new { Checked = "checked", style = "display:none;" })
                            }
                        </td>
                    </tr>
                    <tr>
                        <th>
                        </th>
                        <th>
                        </th>
                        <th>
                        </th>
                        <th>
                        </th>
                        <th>
                        </th>
                        <th>
                        </th>
                        <th>
                        </th>
                     </tr>   
                            index++;
                }
            }

        </table>
        @Html.HiddenFor(model => model.HasCover)
        
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}


